{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fd0a443f",
   "metadata": {},
   "source": [
    "\n",
    "# Numpy for icourse\n",
    "1. np.array() 创建ndarray数组 传入list，使用dtype指定类型，'i1,i4'\n",
    "2. 创建函数\n",
    "|函数|说明|\n",
    "|:-|:-:|\n",
    "|np.arange(n)|类似range()函数，返回ndarray类型，元素从0到n-1|\n",
    "|np.ones(shape)|全1，传入元组|\n",
    "|np.zeros(shape)|全0数组，元组|\n",
    "|np.full(shape,val)|生成指定val数据，shape形状|\n",
    "|np.eye(n)|n*n 单位矩阵，对角线为1，其他0|\n",
    "    -  其他函数\n",
    "    |函数|说明|\n",
    "    |:-|:-:|\n",
    "    |np.linspace()|生成等差数列,n,m,k,起止和数量,指定endpoint=False为不生成最后一个数|\n",
    "    |np.concatenate()|合并成一个新数组|\n",
    "3. 维度变换\n",
    "|方法|说明|\n",
    "|-|-|\n",
    "|.reshape|不改变数组元素，返回一个shape形状的数组，原不变|\n",
    "|.resize(shape)|原变|\n",
    "|.swapaxes(ax1,ax2)|将数组n个维度中两个维度进行调换|\n",
    "|.faltten() |对数组进行降维，返回折叠的一维数组，原不变|\n",
    "4. 类型变换\n",
    "    - .astype(new_type) \n",
    "    - .tolist() 转换成列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "53403f06",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "37db9796",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.5"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一维数组由对等关系（相同类型）的有序或无序数据构成，采用线性方式组织\n",
    "np.array(np.arange(10))\n",
    "x = np.ones((2,3,4))\n",
    "a = np.linspace(1,10,3)\n",
    "a[1] -a[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "946cb244",
   "metadata": {},
   "source": [
    "## 切片\n",
    "1. a[:,:,:] :, 第几个维度，:,行，:,列\n",
    "2. a[::2], 第一个要取出来，中间空一个。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6a5502e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "10 13\n",
      "[ 5 17] [[[ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]]] [[[ 1  2]\n",
      "  [ 5  6]\n",
      "  [ 9 10]]\n",
      "\n",
      " [[13 14]\n",
      "  [17 18]\n",
      "  [21 22]]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array(np.arange(10))\n",
    "# print(arr[1:2])\n",
    "\n",
    "a = np.array(np.arange(24)).reshape((2,3,4))\n",
    "print(a)\n",
    "print(a[0,2,2],a[-1,-3,-3],end='\\n')\n",
    "print(a[:,1,-3],a[:,1:3,:],a[:,:,1:3],end = '\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7468c1e3",
   "metadata": {},
   "source": [
    "## 运算\n",
    "数据与标量之间的运算作用与每一个元素\n",
    "1. NumPy一元函数\n",
    "> 对ndarray中的数据执行元素级运算的函数\n",
    "|函数|说明|\n",
    "|:-|:-|\n",
    "|np.abs(x) np.fabs(x)|各元素的绝对值|\n",
    "|np.sqrt(x)|平方根|\n",
    "|np.square(x)|平方|\n",
    "|np.log(x) np.log10(x) p.log2(x)|自然对数、10底对数和2低对|\n",
    "|np.ceil(x) np.floor(x)|ceiling和floor|\n",
    "|np.rint(x)|四舍五入|\n",
    "|np.modf(x)|各元素的小数和整数部分以两个独立数组新式返回|\n",
    "|np.cos(x) np.cosh(x) <br> np.sin(x) np.sinh <br> np.tan(x) np.tanh|普通和双曲三角函数|\n",
    "|np.exp|指数|\n",
    "|np.sign|各元素符号值 1(+),0,-1(-)|\n",
    "2. 二元函数\n",
    "|函数|说明|\n",
    "|-|-|\n",
    "| +- * / | 运算 |\n",
    "|np.maximum(x,y) np.fmax() <br> np.min...(x,y),np.fmin|最大最小|\n",
    "|np.mod(x,y)|模运算|\n",
    "|np.copysign(x,y) | 将y符号给x |\n",
    "| ><>==!= | 布尔数组 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5b710455",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "swapaxes() missing 1 required positional argument: 'axis2'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[23], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m np\u001b[38;5;241m.\u001b[39mswapaxes(\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m)\n",
      "File \u001b[1;32m<__array_function__ internals>:198\u001b[0m, in \u001b[0;36mswapaxes\u001b[1;34m(*args, **kwargs)\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: swapaxes() missing 1 required positional argument: 'axis2'"
     ]
    }
   ],
   "source": [
    "np.swapaxes(axis1=0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
